#include "addstudentinfo.h"
#include "ui_addstudentinfo.h"

AddStudentInfo::AddStudentInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddStudentInfo)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/img/student.jpg"));
    this->pms = ManagementSystem::GetInstance();
}

AddStudentInfo::~AddStudentInfo()
{
    delete ui;
}


void AddStudentInfo::on_submit_clicked()
{
    string newStuid = ui->stuidEdit->text().toStdString();
    string newUsername = ui->userEdit->text().toStdString();
    StudentInfo dst;
    dst.setStuId(newStuid);
    bool repeat1 = this->pms->rbStuId->search(&dst);
    bool repeat2 = this->pms->rbUsername->search(&dst);
    if (repeat1)
    {
        QMessageBox::critical(this,
               tr("新增失败"),
               tr("新输入学号重复"),
               QMessageBox::Ok | QMessageBox::Cancel,
               QMessageBox::Ok);
    }
    if (repeat2)
    {
        QMessageBox::critical(this,
               tr("新增失败"),
               tr("新输入用户名重复"),
               QMessageBox::Ok | QMessageBox::Cancel,
               QMessageBox::Ok);
    }
    if (!repeat1 && !repeat2)
    {
        string newName = ui->nameEdit->text().toStdString();
        string newGrade = ui->gradeEdit->text().toStdString();
        string newPid = ui->pidEdit->text().toStdString();
        double newScore = ui->scoreEdit->text().toDouble();
        double newCredit = ui->creditEdit->text().toDouble();
        string newPassword = ui->pswEdit->text().toStdString();
        StudentInfo* newValue = new StudentInfo(newName, newStuid, newGrade, newPid, \
                                         newScore, newCredit, newUsername, newPassword);

        this->pms->addInfo(newValue);
        QMessageBox::information(this,
               tr("新增成功"),
               tr("添加新学生信息成功"),
               QMessageBox::Ok | QMessageBox::Cancel,
               QMessageBox::Ok);
        this->hide();
    }
}
